package com.mapapp.flutter_mapapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.constraint.Group;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.adapter.AddPictureAdapter;
import com.mapapp.flutter_mapapp.adapter.SeparateHouseholdAdapter;
import com.mapapp.flutter_mapapp.bean.FixDetailBean;
import com.mapapp.flutter_mapapp.http.Url;
import com.mapapp.flutter_mapapp.persenter.FixSeparationPresenter;
import com.mapapp.flutter_mapapp.util.HorizontalSpaceItemDecoration;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.FixSeparationViewInterface;
import com.mapapp.flutter_mapapp.widget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FixSeparationActivity extends ImmersiveBaseActivity implements FixSeparationViewInterface {


    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.separate_household)
    TextView separateHousehold;
    @BindView(R.id.tv_separate_household)
    TextView tvSeparateHousehold;
    @BindView(R.id.separate)
    TextView separate;
    @BindView(R.id.group_separation)
    Group groupSeparation;
    @BindView(R.id.rv_households)
    RecyclerView rvHouseholds;
    @BindView(R.id.fl_households)
    FrameLayout flHouseholds;
    @BindView(R.id.merge_household)
    TextView mergeHousehold;
    @BindView(R.id.tv_merge_household)
    TextView tvMergeHousehold;
    @BindView(R.id.group_merge)
    Group groupMerge;
    @BindView(R.id.submit_time)
    TextView submitTime;
    @BindView(R.id.tv_submit_time)
    TextView tvSubmitTime;
    @BindView(R.id.add_remark)
    TextView addRemark;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_add_pictures)
    TextView tvAddPictures;
    @BindView(R.id.rv_pictures)
    RecyclerView rvPictures;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.scroll_view)
    NestedScrollView scrollView;
    @BindView(R.id.tv_redo)
    TextView tvRedo;
    private FixSeparationPresenter presenter;
    private AddPictureAdapter pictureAdapter;
    private SeparateHouseholdAdapter separateHouseholdAdapter;
    private final ArrayList<String> imageUrls = new ArrayList<>();
    private final List<String> thumbImageUrls = new ArrayList<>();
    private boolean separation;
    private FixDetailBean.ListBean main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fix_separation);
        ButterKnife.bind(this);
        presenter = new FixSeparationPresenter(this, this, lifecycleSubject);

        String auditId = getIntent().getStringExtra("auditId");
        separation = getIntent().getBooleanExtra("separation", true);


        if (separation) {
            groupMerge.setVisibility(View.GONE);
            titleBar.getTitleBarCenter().setText("分户");
        } else {
            separate.setText("原户");
            titleBar.getTitleBarCenter().setText("合户");
            groupSeparation.setVisibility(View.GONE);
        }
        rvPictures.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        rvPictures.setNestedScrollingEnabled(false);
        pictureAdapter = new AddPictureAdapter(R.layout.item_add_picture);
        rvPictures.addItemDecoration(new HorizontalSpaceItemDecoration(SizeUtils.dp2px(2)));
        rvPictures.setAdapter(pictureAdapter);


        separateHouseholdAdapter = new SeparateHouseholdAdapter(R.layout.item_separate_household);
        rvHouseholds.setLayoutManager(new LinearLayoutManager(mContext));
        rvHouseholds.setAdapter(separateHouseholdAdapter);
        pictureAdapter.makeDelGone();
        pictureAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, PreviewPictureActivity.class);
                intent.putStringArrayListExtra("images", imageUrls);
                intent.putExtra("current", position + 1);
                startActivity(intent);
            }
        });

        titleBar.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        presenter.submitComment(auditId);
    }

    @OnClick(R.id.tv_redo)
    public void onViewClicked() {
        if (main == null) {
            ToastUtils.showMessage(mContext, "数据异常");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("type", 0);
        intent.putExtra("regionId", main.getId());
        intent.putExtra("address", main.getAddress());

        intent.setClass(mContext, NewDataActivity.class);
        startActivity(intent);
    }

    @Override
    public void showResult(FixDetailBean fixDetailBean) {
        if (fixDetailBean != null) {
            List<FixDetailBean.ListBean> old_list = fixDetailBean.getOld_list();
            List<FixDetailBean.ListBean> new_list = fixDetailBean.getNew_list();
            if (separation) {
                if (old_list != null && !old_list.isEmpty()) {
                    FixDetailBean.ListBean oldListBean = old_list.get(0);
                    main = oldListBean;
                    String originalAddress = oldListBean.getAddress();
                    tvSeparateHousehold.setText(originalAddress);
                }

                tvSubmitTime.setText(fixDetailBean.getCreate_time());

                canFixError(fixDetailBean);

                FixDetailBean.PhotoBean photo = fixDetailBean.getPhoto();
                List<String> others = photo.getOthers();
                if (others != null && !others.isEmpty()) {
                    generateImageUrl(others, photo.getPhoto_folder());
                    pictureAdapter.setNewData(thumbImageUrls);
                }

                if (new_list != null && !new_list.isEmpty()) {
                    List<String> items = new ArrayList<>();
                    for (FixDetailBean.ListBean newListBean : new_list) {
                        items.add(newListBean.getAddress());
                    }
                    separateHouseholdAdapter.setNewData(items);
                }
            } else {
                if (new_list != null && !new_list.isEmpty()) {
                    FixDetailBean.ListBean newListBean = new_list.get(0);
                    String originalAddress = newListBean.getAddress();
                    main = newListBean;
                    tvMergeHousehold.setText(originalAddress);
                }

                tvSubmitTime.setText(fixDetailBean.getCreate_time());

                canFixError(fixDetailBean);

                FixDetailBean.PhotoBean photo = fixDetailBean.getPhoto();
                List<String> others = photo.getOthers();
                if (others != null && !others.isEmpty()) {
                    generateImageUrl(others, photo.getPhoto_folder());
                    pictureAdapter.setNewData(thumbImageUrls);
                }

                if (old_list != null && !old_list.isEmpty()) {
                    List<String> items = new ArrayList<>();
                    for (FixDetailBean.ListBean oldListBean : old_list) {
                        items.add(oldListBean.getAddress());
                    }
                    separateHouseholdAdapter.setNewData(items);
                }
            }

        }
    }

    private void canFixError(FixDetailBean fixDetailBean) {
        if (fixDetailBean == null) {
            return;
        }
        switch (fixDetailBean.getState()) {
            case 0:  //未审核
                tvResult.setTextColor(Color.parseColor("#65A9E8"));
                tvResult.setText("尚未审核");
                tvRedo.setEnabled(false);
                tvRedo.setSelected(true);
                break;

            case 1:  //通过
                tvResult.setTextColor(Color.parseColor("#62B4AE"));
                tvResult.setText("审核通过");
                tvRedo.setEnabled(false);
                tvRedo.setSelected(true);
                break;

            case 2:  //驳回
                tvResult.setTextColor(Color.parseColor("#E05959"));
                tvResult.setText(String.format("驳回原因：%s", fixDetailBean.getReject_reason()));
                break;

            case 3:  //撤销
                tvResult.setTextColor(Color.parseColor("#4C96DC"));
                tvResult.setText("审核撤销");
                tvRedo.setEnabled(false);
                tvRedo.setSelected(true);
                break;
        }
    }


    private void generateImageUrl(List<String> list, String dirName) {
        imageUrls.clear();
        thumbImageUrls.clear();
        for (String s : list) {
            if (s != null && !s.startsWith("top_")) {
                String prefix = Url.BASE_URL + "/common/output/image/" +
                        dirName + "/" + s + "/";
                imageUrls.add(prefix + "ygf");
                thumbImageUrls.add(prefix + "thumb");
            }
        }
    }

}
